package techguy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import beans.TechPageBean;
import dtos.ResponseDTO;
import dtos.TechticketDTO;
import dtos.TechticketTechguyDTO;
import dtos.UserDTO;
import utils.DomainDAORemote;
import utils.ResponseDAORemote;
import utils.TechresourceDAORemote;
import utils.TechticketDAORemote;
import utils.UserDAORemote;
import utils.UsertypeDAORemote;

/**
 * 
 * @author figans
 * 
 */
@ManagedBean
@SessionScoped
public class ViewTicketsBean {

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	@EJB
	TechticketDAORemote techticketDAO;

	@EJB
	UserDAORemote userDAO;

	@EJB
	UsertypeDAORemote usertypeDAO;

	@EJB
	DomainDAORemote domainDAO;

	@EJB
	TechresourceDAORemote techresourceDAO;

	@EJB
	ResponseDAORemote responseDAO;

	List<TechticketTechguyDTO> techticketList;

	TechticketTechguyDTO ticket;

	int ticketID;

	String response;

	boolean emptyList;

	@ManagedProperty("#{techPageBean}")
	TechPageBean tpb;

	String message;
	
	private String msgRespondSent = bundle.getString("Beans_techguy_viewTicketsBean_msgRespondSent");
	
	public String getMsgRespondSent() {
		return msgRespondSent;
	}

	public void setMsgRespondSent(String msgRespondSent) {
		this.msgRespondSent = msgRespondSent;
	}

	@PostConstruct
	private void init(){
		message = "";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public TechPageBean getTpb() {
		return tpb;
	}

	public void setTpb(TechPageBean tpb) {
		this.tpb = tpb;
	}

	/**
	 * Method that checks if the list of techtickets is empty or not
	 * 
	 * @return true/false
	 */
	public boolean getEmptyList() {
		if (getTechticketList().isEmpty()) {
			this.emptyList = false;
		} else {
			this.emptyList = true;
		}
		return emptyList;
	}

	public void setEmptyList(boolean emptyList) {
		this.emptyList = emptyList;
	}

	/**
	 * Getter for the ticketlist attribute. If not null converts all the tickets
	 * into special Techguy tickets and adds them to a list
	 * 
	 * @return a list of techguy tickets
	 */
	public List<TechticketTechguyDTO> getTechticketList() {

		techticketList = new ArrayList<>();
		List<TechticketDTO> techticketL = new ArrayList<>();
		techticketL = techticketDAO.getAllTechticketsTG();

		for (TechticketDTO temp : techticketL) {
			System.out.println(temp.getIdResponse());
			if (temp.getIdResponse() == 0) {
				techticketList.add(toTechticketTguy(temp));
			}

		}

		return techticketList;
	}

	public void setTechticketList(List<TechticketTechguyDTO> techticketList) {
		this.techticketList = techticketList;
	}

	/**
	 * Getter for the ticket object. Returns all the details of the ticket for
	 * which details are requested
	 * 
	 * @return a ticket object
	 */
	public TechticketTechguyDTO getTicket() {
		if (FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("ticketID") == null) {
			this.ticketID = Integer.parseInt(FacesContext.getCurrentInstance()
					.getExternalContext().getSessionMap().get("ticketID")
					.toString());
		} else {
			this.ticketID = Integer.parseInt(FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap()
					.get("ticketID").toString());
			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().put("ticketID", ticketID);
		}
		if (ticket == null) {
			ticket = new TechticketTechguyDTO();
			for (TechticketTechguyDTO temp : getTechticketList()) {
				if (temp.getIdTicket() == ticketID) {
					this.ticket = temp;
				}
			}
		}
		message = "";
		return ticket;
	}

	public void settingTicket() {
		message = "";
		tpb.setPage("ticketDetail");
	}

	public void setTicket(TechticketTechguyDTO ticket) {
		this.ticket = ticket;
	}

	public String getResponse() {
		return "";
	}

	public void setResponse(String response) {
		this.response = response;
	}

	/**
	 * A method that adds a response to the ticket
	 * 
	 * @return
	 */
	public void addResponse() {

		ResponseDTO responseDTO = new ResponseDTO();
		/*
		 * La integrare trebuie decomentate liniile de mai jos
		 */
		 UserDTO ud = (UserDTO)
		 FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
		 .get("loggedUser");
		
		responseDTO.setIdUser(ud.getId());
		responseDTO.setDescription(response);
		responseDTO.setDate(new Date());

		responseDAO.insertResponse(responseDTO);

		TechticketDTO fullticket = techticketDAO.getTechticket(Integer
				.parseInt(FacesContext.getCurrentInstance()
						.getExternalContext().getSessionMap().get("ticketID")
						.toString()));

		List<ResponseDTO> responseList = responseDAO.getAllResponses();
		for (ResponseDTO temp : responseList) {
			if (responseDTO.getDate().equals(temp.getDate())) {
				fullticket.setIdResponse(temp.getId());
			}
		}
		fullticket.setStatus("Responded");
		techticketDAO.updateTechticket(fullticket);
		techticketList = null;
		message = msgRespondSent;
		tpb.setPage("ticketView");
	}

	public void goBack() {
		tpb.setPage("ticketView");
	}

	/**
	 * Takes a ticket and converts it to a tech guy tech ticket
	 * 
	 * @param ticket
	 *            Techticket object
	 * @return a tech guy techticket object
	 */
	private TechticketTechguyDTO toTechticketTguy(TechticketDTO ticket) {
		TechticketTechguyDTO t = new TechticketTechguyDTO();
		t.setIdTicket(ticket.getId());
		t.setSenderUsername(userDAO.getUser(ticket.getIdUser()).getUsername());
		t.setSenderName((userDAO.getUser(ticket.getIdUser()).getName()));
		t.setSenderUsertype(usertypeDAO.getUsertype(
				userDAO.getUser(ticket.getIdUser()).getIdUsertype()).getName());
		t.setSenderDomain(domainDAO.getDomain(
				userDAO.getUser(ticket.getIdUser()).getIdDomain()).getName());
		t.setResProductID(techresourceDAO.getTechresourcesById(
				ticket.getIdTechresource()).getId());
		t.setResProductName(techresourceDAO.getTechresourcesById(
				ticket.getIdTechresource()).getName());
		t.setResProductDomain((domainDAO.getDomain(techresourceDAO
				.getTechresourcesById(ticket.getIdTechresource()).getIdDomain())
				.getName()));
		t.setProblemType(ticket.getProblemtype());
		t.setDescription(ticket.getDescription());
		t.setDate(ticket.getDate());
		return t;
	}
}
